package com.tengju.bff.interfaces.covercharge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tengju.bff.interfaces.covercharge.constant.SelfWithdrawStatusEnum;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceInvoiceShippingLog;
import com.tengju.bff.interfaces.covercharge.entity.po.ServiceSelfCertificationWithdraw;
import com.tengju.bff.interfaces.covercharge.entity.vo.InvoiceVo;
import com.tengju.bff.interfaces.covercharge.mapper.ServiceInvoiceShippingLogMapper;
import com.tengju.bff.interfaces.covercharge.service.IServiceInvoiceShippingLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tengju.bff.interfaces.covercharge.service.IServiceSelfCertificationWithdrawService;
import com.zoi7.component.core.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 自认证服务费提现发票快递信息表 服务实现类
 * </p>
 *
 * @author fwy
 * @since 2020-03-09
 */
@Service
public class ServiceInvoiceShippingLogServiceImpl extends ServiceImpl<ServiceInvoiceShippingLogMapper, ServiceInvoiceShippingLog> implements IServiceInvoiceShippingLogService {

    @Autowired
    private IServiceSelfCertificationWithdrawService serviceSelfCertificationWithdrawService;

    @Autowired
    @Lazy
    private IServiceInvoiceShippingLogService serviceInvoiceShippingLogService;

    @Autowired
    private ServiceInvoiceShippingLogMapper serviceInvoiceShippingLogMapper;

    /**
     *
     * @param invoiceVo 邮寄发票信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void apply(InvoiceVo invoiceVo){
        //查询自认证提现申请记录
        ServiceSelfCertificationWithdraw withdraw = serviceSelfCertificationWithdrawService.getById(invoiceVo.getWithdrawId());
        if(withdraw == null){
            throw new ServiceException("没有找到该条申请记录");
        }
        if(!(SelfWithdrawStatusEnum.FPASS.getCode().equals(withdraw.getStatus()) ||
                SelfWithdrawStatusEnum.SREJECT.getCode().equals(withdraw.getStatus()))){
            throw new ServiceException("该申请不符合邮寄发票条件");
        }
        //修改审批记录状态
        ServiceSelfCertificationWithdraw updateWithdraw = new ServiceSelfCertificationWithdraw();
        updateWithdraw.setId(withdraw.getId());
        updateWithdraw.setStatus(SelfWithdrawStatusEnum.SEND.getCode());
        updateWithdraw.setUpdatedAt(LocalDateTime.now());
        serviceSelfCertificationWithdrawService.saveOrUpdate(updateWithdraw);

        //新增发票信息
        ServiceInvoiceShippingLog log = new ServiceInvoiceShippingLog();
        //查询是否已经存在提现发票信息
        QueryWrapper<ServiceInvoiceShippingLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("self_withdraw_id",invoiceVo.getWithdrawId());
        queryWrapper.eq("type",1);
        ServiceInvoiceShippingLog checklog = serviceInvoiceShippingLogMapper.selectOne(queryWrapper);
        if(checklog != null){
            log.setId(checklog.getId());
        }
        log.setExpressName(invoiceVo.getExpressName());
        log.setImgId(invoiceVo.getImgId());
        log.setShippingNo(invoiceVo.getShippingNo());
        log.setSelfWithdrawId(invoiceVo.getWithdrawId());
        log.setUpdatedAt(LocalDateTime.now());
        log.setType(1);
        serviceInvoiceShippingLogService.saveOrUpdate(log);
    }

}
